package offer.algorithm;

import offer.singleLinkedList.Node;

/**
 * Description：删除有序单链表中的重复节点
 * Author: zhangc
 * Date：2017/7/28 19:35
 */
public class DeleteRepeatNode57 {

    private static <T> Node<T> removeRepeatNode(Node<T> head) {
        if (head == null || head.next == null || !head.value.equals(head.next.value))
            return head;
        Node<T> headTemp = head.next;
        while (head != null && head.value.equals(headTemp.value)) {
            while (headTemp != null && headTemp.value.equals(head.value)) {
                headTemp = headTemp.next;
            }
            head = headTemp;
            if (head != null)
                headTemp = head.next;
        }
        return head;
    }

    public static <T> Node<T> removeRepeatNodeList(Node<T> head) {
        Node<T> tNode = removeRepeatNode(head);
        Node<T> temp = tNode;
        while (temp != null && temp.next != null) {
            temp.next = removeRepeatNode(temp.next);
            temp = temp.next;
        }
        return tNode;
    }
}
